<template>
  <a-spin :spinning="confirmLoading" style="background: #fff">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12" :hidden="true">
            <a-form-model-item label="流程名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bpmBizTitle" >
              <a-input :max-length="255" :disabled="formDisabled" v-model="model.bpmBizTitle" placeholder="请输入流程名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="单据编号">
              <a-input placeholder="请输入单据编号" v-model="model.billNo" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="申请时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
              <j-date
                placeholder="请选择申请时间"
                v-model="model.createTime"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm"
                style="width: 100%"
                disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="申请人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proposer">
              <!--              <select-user-by-dep-->
              <!--                :disabled=formDisabled-->
              <!--                v-model="model.proposer" disabled/>-->
              <select-user-by-dep :multi="false" v-model="model.proposer" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="申请部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proposerDeptId">
              <j-select-depart v-model="model.proposerDeptId" disabled/>
            </a-form-model-item>
          </a-col>
          <a-divider>基本信息</a-divider>
          <a-col :span="12">
            <a-form-model-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mc">
              <a-input v-if="!formDisabled" :disabled="formDisabled" v-model="model.mc" placeholder="请输入项目名称"></a-input>
              <p style="display: flex; align-items: center; justify-content: center; margin: 0;" v-else>
                <a-input :disabled="formDisabled" v-model="model.mc" placeholder="请输入项目名称"></a-input>
                <a-button v-if="formData && formData.tableValue && formData.tableValue == '待办'" @click.stop="onCheckProjectName(model.mc)" type="danger" ghost>校重检验</a-button>
              </p>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="12">-->
          <!--            <a-form-model-item label="项目类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmlx">-->
          <!--              <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.xmlx" dictCode="xmlx_type"-->
          <!--                                 placeholder="请选择项目类型"/>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="12">
            <a-form-model-item label="经营团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ywtd">
              <!-- <a-input :disabled=formDisabled v-model="model.ywtd" placeholder="请输入业务团队"  ></a-input>-->
              <select-team-input
                @back="changeVal"
                :disabled="formDisabled"
                v-model="model.ywtd"
                :tdlx="1"/>

            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="经营公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessCompanyId">
              <select-Company-input :disabled="formDisabled" placeholder="请选择经营公司" v-model="model.businessCompanyId" disabled /> 
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="项目承接人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cjr">
              <!--              <select-user-by-dep-->
              <!--                  :disabled=formDisabled-->
              <!--                  v-model="model.cjr"/>-->
              <select-user-by-dep :disabled="formDisabled" :multi="false" v-model="model.cjr" />

            </a-form-model-item>
          </a-col>
          <!--task#21182118 立项和预立项管理中的是否招投标改为是否是否投标、报价；-->
          <!-- <a-col :span="12">
            <a-form-model-item label="是否投标、报价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sfztb">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="radio"
                v-model="model.sfztb"
                dictCode="yn"
                placeholder="请选择是否是否投标、报价"/>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="是否环评项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sfhpxm">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="radio"
                v-model="model.sfhpxm"
                dictCode="yn"
                placeholder="请选择是否环评项目"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="签订单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qddw">
              <!--              <j-select-company :disabled=formDisabled placeholder="请输入签订单位" v-model="model.qddw" />-->
              <select-Company-input placeholder="请输入签订单位" :disabled="formDisabled" @change="handleSealChange" v-model="model.qddw"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="业绩归属年度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="yjgsnd">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="list"
                v-model="model.yjgsnd"
                dictCode="yjgsnd"
                placeholder="请选择业绩归属年度"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="业主单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="yzdw">
              <!--<a-input :disabled=formDisabled v-model="model.yzdw" placeholder="请输入业主单位"  ></a-input>-->
              <select-customer-input
                :disabled="formDisabled"
                v-model="model.yzdw"
                commercialType="1"
                @change="yzdwChange"/>

              <!--hmhb_park,park_name,id,del_flag=0 and bpm_status=3-->

            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lxr">
              <!--<a-input :disabled=formDisabled v-model="model.lxr" placeholder="请输入联系人"  ></a-input>-->
              <j-dict-select-tag
                :disabled="formDisabled"
                type="list"
                v-model="model.lxr"
                :dictCode="cust_lxr"
                placeholder="请选择联系人"
                @input="lxrChange"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lxdh">
              <a-input :disabled="formDisabled" v-model="model.lxdh" placeholder="请输入联系电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="项目所在地址" :labelCol="labelCol" :wrapperCol="wrapperCol" style="height:75px" prop="projectAddress">
              <a-form-model-item prop="ssq" :labelCol="labelCol" :wrapperCol="wrapperCol" :style="{display:'inline-block',width:'48%'}">
                <AddressChoice :disabled="formDisabled" type="cascader" v-model="model.ssq" placeholder="请输入省市区" />
              </a-form-model-item>
              <a-form-model-item prop="projectAddress" :labelCol="labelCol" :wrapperCol="wrapperCol" :style="{'margin-top':'0px','margin-left':'5px',display:'inline-block',width:'50%'}">
                <a-input :disabled="formDisabled" v-model="model.projectAddress" placeholder="请输入项目详细地址"></a-input>
              </a-form-model-item>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="项目来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xmly">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="list"
                v-model="model.xmly"
                dictCode="xmly_type"
                placeholder="请选择项目来源"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="预计合同额(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="yjhte">
              <a-input-number
                :disabled="formDisabled"
                :max="99999999"
                :min="0"
                :precision="2"
                v-model="model.yjhte"
                placeholder="请输入预计合同额"
                style="width: 100%"
                @blur="convertMoney"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="大写" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dx">
              <a-input :disabled="true" v-model="model.dx" placeholder="请输入大写"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="立项事由及说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lxsyjsm">
              <a-textarea :max-length="500" :disabled="formDisabled" v-model="model.lxsyjsm" rows="4" placeholder="规划面积、主导产业、企业总数量、区域分布等基本情况"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="相关附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xgfj">
              <!--<j-upload :disabled=formDisabled v-model="model.xgfj"   ></j-upload>-->
              <j-upload v-if="!formDisabled" :disabled="formDisabled" v-model="model.xgfj"></j-upload>
              <a-button v-else icon="download" @click="viewFile(model.xgfj)">文件列表</a-button>
            </a-form-model-item>
          </a-col>

          <a-col :span="24" style="margin-bottom: 24px">
            <div style="margin-bottom: 6px;margin-top: 6px">
              <a-button @click="handleAdd" v-if="!formDisabled" type="primary">新增</a-button>
            </div>
            <a-table
              ref="table"
              size="middle"
              rowKey="id"
              :loading="loading"
              :columns="columns"
              :pagination="false"
              :data-source="dataSource">
              <span slot="action" slot-scope="text, record">
                <template v-if="!formDisabled">
                  <a-button
                    class="ruleButton"
                    :ghost="true"
                    type="primary"
                    icon="edit"
                    @click="handleEdit(record)"/>
                  <a-divider type="vertical"/>
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)" :getPopupContainer="(node) => node.parentNode">
                    <a-button
                      class="ruleButton"
                      :ghost="true"
                      type="primary"
                      icon="delete"
                    />
                  </a-popconfirm>
                </template>
              </span>
            </a-table>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="报价文件用印" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isSeal">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="radio"
                v-model="model.isSeal"
                dictCode="yn"
                @input="handleChangeSeal"
                placeholder="请选择报价文件用印"/>
            </a-form-model-item>
          </a-col>
          <!-- 当报价文件用印为是时 -->
          <template v-if="model.isSeal == '1'">
            <a-col :span="12">
              <a-form-model-item label="用印类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="signSealWay">
                <j-dict-select-tag
                  :disabled="formDisabled"
                  type="select"
                  v-model="model.signSealWay"
                  dictCode="seal_way"
                  placeholder="请选择用印类型"/>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="是否借出" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lendYn">
                <j-dict-select-tag
                  :disabled="formDisabled"
                  type="radio"
                  v-model="model.lendYn"
                  dictCode="yes_no"
                  placeholder="请选择是否借出"/>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="文件份数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quantity">
                <a-input-number
                  :disabled="formDisabled"
                  :max="99999999"
                  :min="0"
                  v-model="model.quantity"
                  placeholder="请输入文件份数"
                  style="width: 100%"/>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="用印说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userExplain">
                <a-textarea :disabled="formDisabled" v-model="model.userExplain" rows="4" placeholder="请输入用印说明" :maxLength="500"/>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="选择印章" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sealIds">
                <a-tooltip placement="topLeft">
                  <template slot="title" v-if="!model.qddw">
                    <span>请选择签订单位</span>
                  </template>
                  <select-normal-seal-input
                    ref="sealInput"
                    :company="model.qddw"
                    :disabled="formDisabled ? formDisabled : !model.qddw"
                    v-model="model.sealIds"/>
                </a-tooltip>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="signFile">
                <template v-slot:label>
                  <span >用印文件</span><span style="color: #faad14;font-weight: 600">（请上传只需盖章的用印文件！）</span>
                </template>
                <j-upload
                  v-if="!formDisabled"
                  :disabled="formDisabled"
                  v-model="model.signFile"
                  :multiple="false"
                  fileFormat=".pdf, .PDF, .doc, .docx"
                  fileFormatText="请上传word与pdf格式的文件！"
                  fileType="file"
                  :fillSize="50"
                  :isSingle="true"
                ></j-upload>
                <a-button v-else icon="download" @click="viewFile(model.signFile)">文件列表</a-button>
              </a-form-model-item>
            </a-col>
          </template>

          <!-- <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col> -->
        </a-row>
      </a-form-model>
      <common-flow-list v-if="formBpm && model.bpmStatus !== '1'" :formData="formData" style="margin-top: 10px"></common-flow-list>
      <view-file-modal ref="viewFileModal"></view-file-modal>
      <erp-omc-prepare-project-item-modal ref="itemList" @ok="modalFormOk"></erp-omc-prepare-project-item-modal>
    </j-form-container>
    <a-row :gutter="48">
      <a-col :span="24" style="text-align: center;margin-top: 15px">
        <a-button type="primary" v-if="formBpm" :style="{ marginRight: showFlowSubmitButton?'10px':'0px' }" @click="onPrint">打印</a-button>
        <a-button @click="submitForm('submit')" v-if="showFlowSubmitButton">提 交</a-button>
      </a-col>
    </a-row>
    <ErpOmcPrepareProjectNameCheckModal ref="ErpOmcPrepareProject"></ErpOmcPrepareProjectNameCheckModal>
    <ErpOmcPrepareProjectFormPrint class="print_style" v-if="formBpm" id="prepareContent" :formData="model" :formDataHistory="formData"></ErpOmcPrepareProjectFormPrint>
  </a-spin>
</template>

<script>

import { getAction, httpAction } from '@/api/manage'
import { viewFile } from '@/utils/util'
import { DisabledAuthFilterMixin } from '@/mixins/DisabledAuthFilterMixin'
import ViewFileModal from '@/views/scy/components/ViewFileModal'
import ErpOmcPrepareProjectItemModal from '../modules/ErpOmcPrepareProjectItemModal'
import { filterMultiDictText, initDictOptions } from '@/components/dict/JDictSelectUtil'
import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
import SelectCustomerInput from '@/views/scy/components/SelectCustomerInput'
import { convertCurrency } from '@/utils/MoneyConvert'
import AddressChoice from '@/components/AddressChoice/AddressChoice';
import ErpOmcPrepareProjectNameCheckModal from './ErpOmcPrepareProjectNameCheckModal' // 项目名称校验
import SelectNormalSealInput from '@/views/erp/administration/seal/modules/SelectNormalSealInput';
import { mapGetters } from 'vuex';
import moment from 'moment';
import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
import CommonFlowList from '@/views/components/CommonFlowList.vue';
import Print from '@/utils/print/printarea.js';
import ErpOmcPrepareProjectFormPrint from './ErpOmcPrepareProjectFormPrint' // 打印
// 校验项目名称是否唯一
let checkProjectName = (rule, value, callback) => {
  if (!value) {
    return
  }
  let params = {
    projectName: value,
    type: 'prepare' // 立项：project  预立项：prepare
  }
  getAction('/api/erp/omc/project/erpOmcProject/checkProjectDuplicate', params).then(res => {
    if (res.success) {
      callback();
    } else {
      callback(new Error(res.message));
    }
  }).catch(error => {
    callback(new Error(error.message));
  })
}
export default {
  name: 'ErpOmcPrepareProjectForm',
  mixins: [DisabledAuthFilterMixin, viewFile],
  components: {
    ErpOmcPrepareProjectItemModal,
    ViewFileModal,
    SelectUserByDep,
    SelectTeamInput,
    SelectCustomerInput,
    AddressChoice,
    SelectCompanyInput,
    CommonFlowList,
    ErpOmcPrepareProjectFormPrint,
    ErpOmcPrepareProjectNameCheckModal,
    SelectNormalSealInput
  },
  props: {
    // 流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    // 表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        // sfztb: undefined,
        sfhpxm: undefined
      },
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      confirmLoading: false,
      validatorRules: {
        // task#2118 是否环评改为必填项，默认为否
        sfhpxm: [
          { required: true, message: '请选择是否环评!' }
        ],
        // sfztb: [
        //   { required: true, message: '请选择是否是否投标、报价!' }
        // ],
        mc: [
          { required: true, message: '请输入项目名称!', pattern: '[^ \x22]+' },
          { validator: checkProjectName, trigger: 'blur' }
        ],
        ywtd: [
          { required: true, message: '请输入经营团队!' }
        ],
        cjr: [
          { required: true, message: '请输入项目承接人!' }
        ],
        qddw: [
          { required: true, message: '请输入签订单位!' }
        ],
        yjgsnd: [
          { required: true, message: '请输入业绩归属年度!' }
        ],
        yzdw: [
          { required: true, message: '请输入业主单位!' }
        ],
        lxr: [
          { required: true, message: '请输入联系人!' }
        ],
        lxdh: [
          { required: true, message: '请输入联系电话!' },
          { pattern: /(^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$)|(^\/$)/, message: '请输入正确的联系电话!' }
        ],
        ssq: [
          { required: true, message: '请输入省市区!' }
        ],
        projectAddress: [
          { required: true, message: '请输入详细地址!' }
        ],
        xmly: [
          { required: true, message: '请输入项目来源!' }
        ],
        yjhte: [
          { required: true, message: '请输入预计合同额(元)!' }
        ],
        isSeal: [
          { required: true, message: '请选择报价文件用印!' }
        ],
        signSealWay: [
          { required: true, message: '请选择用印类型!' }
        ],
        lendYn: [
          { required: true, message: '请选择是否借出!' }
        ],
        quantity: [
          { required: true, message: '请输入文件份数!' }
        ],
        userExplain: [
          { required: true, message: '请输入用印说明!' }
        ],
        sealIds: [
          { required: true, message: '请选择印章!' }
        ],
        signFile: [
          { required: true, message: '请选择用印文件!' }
        ],
        // task#2118 立项事由及说明非必填
        // lxsyjsm: [
        //   { required: true, message: '请输入立项事由及说明!' },
        // ],
      },
      url: {
        add: '/api/erp/omc/prepareproject/erpOmcPrepareProject/add',
        edit: '/api/erp/omc/prepareproject/erpOmcPrepareProject/edit',
        queryById: '/api/erp/omc/prepareproject/erpOmcPrepareProject/queryById',
        queryItemList: '/api/erp/omc/prepareproject/erpOmcPrepareProjectItem/listAll',
        generatedFormNumber: '/api/erp/base/generatedFormNumber'
      },
      loading: false,
      columns: [
        {
          title: '业务编码',
          align: 'center',
          dataIndex: 'ywfl',
          customRender: (text) => {
            if (!text) {
              return ''
            } else {
              return filterMultiDictText(this.dictOptions['ywfl'], text + '')
            }
          }
        },
        {
          title: '业务名称',
          align: 'center',
          dataIndex: 'ywmc'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: {
            filterDropdown: 'filterDropdown',
            filterIcon: 'filterIcon',
            customRender: 'action'
          }
        }
      ],
      dataSource: [],
      dictOptions: {},
      cust_lxr: 'erp_omc_customer_manager_contact,lxr,id,del_flag=0 and main_id=0'
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    // 如果是流程中表单，则需要加载流程表单data
    this.showFlowData();

    this.initDictConfig();
    if (this.formDisabled) {
      this.columns.pop()
    }
  },
  methods: {
    ...mapGetters(['userInfo']),
    // 点击项目名称按钮校验
    onCheckProjectName (value) {
      this.$refs.ErpOmcPrepareProject.getDetail()
    },
    generatedFormNumber() {
      getAction(this.url.generatedFormNumber, { prefix: 'YLX', subSystemCode: '05' }).then(res => {
        if (res.success) {
          this.$set(this.model, 'billNo', res.result)
        }
      })
    },

    changeVal(val){
      this.$set(this.model, 'businessCompanyId',val[0].belongCompanyId)
    },

    /*
    印章所属公司改变清空印章列表
     */
    handleSealChange() {
      // 判断报价文件用印是否为1
      if (this.model.isSeal == '1') {
        this.$nextTick(() => {
          this.$refs.sealInput.resetObj()
          this.$delete(this.model, 'sealIds')
        })
      }
    },
    // 选择保价文件用印
    handleChangeSeal(val) {
      if (val == '1') {
        this.$nextTick(() => {
          if (this.model.qddw) {
            this.$refs.sealInput.resetObj()
          }
          this.$refs.form.clearValidate(['signSealWay', 'lendYn', 'quantity', 'userExplain', 'sealIds', 'signFile']);
        })
      }
      this.$set(this.model, 'signSealWay', null)
      this.$set(this.model, 'lendYn', null)
      this.$set(this.model, 'quantity', null)
      this.$set(this.model, 'userExplain', null)
      this.$set(this.model, 'sealIds', null)
      this.$set(this.model, 'signFile', null)
    },
    yzdwChange() {
      this.$set(this.model, 'lxr', null)
      this.$set(this.model, 'lxdh', null)
      this.cust_lxr = 'erp_omc_customer_manager_contact,lxr,id,del_flag=0 and main_id=\'' + this.model.yzdw + '\'';
    },
    lxrChange(val) {
      // debugger
      getAction('/api/erp/omc/customermanager/erpOmcCustomerManagerContact/queryById', { id: val }).then((res) => {
        if (res.success) {
          let data = res.result.records || res.result
          // debugger;
          // this.model.lxdh = data.lxfs;
          this.$set(this.model, 'lxr', val)
          this.$set(this.model, 'lxdh', data.lxfs)
        }
      })
    },
    initDictConfig() {
      initDictOptions('ywfx_type').then((res) => {
        if (res.success) {
          this.$set(this.dictOptions, 'ywfl', res.result)
        }
      })
    },
    handleAdd() {
      this.$refs.itemList.add();
      this.$refs.itemList.title = '新增';
      this.$refs.itemList.disableSubmit = false;
    },
    handleEdit(record) {
      this.$refs.itemList.edit(record);
      this.cust_lxr = 'erp_omc_customer_manager_contact,lxr,id,del_flag=0 and main_id=\'' + this.model.yzdw + '\'';
      this.$refs.itemList.title = '修改';
      this.$refs.itemList.disableSubmit = false;
    },
    handleDelete(id) {
      for (let i = 0; i < this.dataSource.length; i++) {
        if (this.dataSource[i].id == id) {
          this.dataSource.splice(i, 1);
        }
      }
    },
    modalFormOk(data) {
      this.loading = true;
      let flag = true;
      // debugger;
      for (let i = 0; i < this.dataSource.length; i++) {
        if (this.dataSource[i].id == data.id) {
          this.dataSource.splice(i, 1, data);
          flag = false;
        }
      }
      if (flag) {
        this.dataSource.push(data);
      }
      this.loading = false;
    },
    add() {
      this.dataSource = []
      this.edit(this.modelDefault);
      this.userName = this.userInfo().empName;// 申请人名称
      this.model.proposer = this.userInfo().empId; // 申请人
      this.model.proposerDeptId = this.userInfo().deptId; // 申请部门
      this.model.createTime = moment().format('YYYY-MM-DD HH:mm');
      this.$set(this.model, 'isSeal', '0')
      this.generatedFormNumber();
    },
    edit(record) {
      this.$refs.form.resetFields();
      this.model = Object.assign({}, record);
      if (this.model.yjhte) {
        this.$set(this.model, 'dx', convertCurrency(this.model.yjhte))
      }
      if (this.model.id) {
        getAction(this.url.queryItemList, { mainId: this.model.id }).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records || res.result
          }
        })
      }
      // this.yzdwChange();
      this.cust_lxr = 'erp_omc_customer_manager_contact,lxr,id,del_flag=0 and main_id=\'' + this.model.yzdw + '\'';
      this.visible = true;
    },
    // 渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId };
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result);
          }
        });
      }
    },

    submitForm(type) {
      const that = this;
      if (type == 'draft') {
        // 代表保存草稿不做校验
        this.submit(type)
      } else if (type == 'submit') {
        // 触发表单验证
        if (this.dataSource == null || this.dataSource.length <= 0) {
          that.$message.warning('必须填写业务分类信息！');
          that.$emit('unButtonDisable')
          return
        }
        this.$refs.form.validate(valid => {
          if (valid) {
            that.submit(type)
          } else {
            that.$emit('unButtonDisable')
          }
        })
      } else {
        console.log('传参数，保存：draft，提交：submit')
      }
    },
    submit (type) {
      const that = this;
      that.confirmLoading = true;
      let httpurl = '';
      let method = '';
      if (!this.model.id) {
        httpurl += this.url.add;
        method = 'post';
      } else {
        httpurl += this.url.edit;
        method = 'put';
      }
      this.model.itemList = this.dataSource || []
      httpAction(httpurl, this.model, method).then((res) => {
        if (res.success) {
          that.$message.success(res.message);
          if (method == 'post') {
            that.model.id = res.result;
          }
          that.$emit('ok', type, that.model);
        } else {
          that.$message.warning(res.message);
          that.$emit('unButtonDisable');
        }
      }).catch((error) => {
        that.$emit('unButtonDisable', error);
      }).finally(() => {
        that.confirmLoading = false;
      })
    },

    convertMoney() {
      let money = this.model.yjhte
      this.$set(this.model, 'dx', convertCurrency(money))
    },

    // 点击打印
    onPrint () {
        let print = new Print({
          el: '#prepareContent',
          popTitle: '预立项申请详情 · 禾美环保',
          endCallback() {

          }
        })
      }

  }
}
</script>
<style lang="less" scoped>
.print_style{
  position: absolute;
  top: 0;
  z-index: -1111;
}
</style>
<style>
  @import "../../../../../utils/print.css";
</style>
